﻿using System;
using Firestarter.Test.Base;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO;
using NUnit.Framework;

namespace FireStarterModeller.Test.BO.BusinessObjectRules
{
    [TestFixture]
    public class TestDeleteActionRestrictionsRule
    {
        [SetUp]
        public void SetupTest()
        {
            //Runs every time that any testmethod is executed
            TestUtilsDomain.SetupTest();
            BORegistry.DataAccessor = new DataAccessorInMemory();
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            //Code that is executed before any test is run in this class. If multiple tests
            // are executed then it will still only be called once.
            TestUtilsDomain.SetupFixture();
        }

        [Test]
        public void Test_CreateDMRelationshipDeleteActionRule()
        {
            //---------------Set up test pack-------------------
            DMRelationship dmRelationship = TestUtilsDMRelationship.CreateUnsavedDefaultDMRelationship();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IBusinessObjectRule rule = new DMRelationshipDeleteActionRule();
            //---------------Test Result -----------------------
            Assert.IsNotNull(rule);
            Assert.AreEqual("DMRelationshipDeleteActionRule", rule.Name);
            Assert.AreEqual(ErrorLevel.Error, rule.ErrorLevel);
        }

        [Test]
        public void Test_CreateDMRelationshipDeleteActionRule_NoClassDM()
        {
            //---------------Set up test pack-------------------
            DMRelationship dmRelationship = TestUtilsDMRelationship.CreateUnsavedDefaultDMRelationship();
            dmRelationship.ClassDM = null;
            //---------------Assert Precondition----------------
            Assert.IsNull(dmRelationship.ClassDM);
            //---------------Execute Test ----------------------
            IBusinessObjectRule rule = new DMRelationshipDeleteActionRule();
            rule.IsValid(dmRelationship);
            //---------------Test Result -----------------------
            Assert.IsNotNull(rule);
            Assert.AreEqual("DMRelationshipDeleteActionRule", rule.Name);
            Assert.AreEqual(ErrorLevel.Error, rule.ErrorLevel);
            StringAssert.Contains("The Delete Action on Relationship ", rule.Message);
            StringAssert.Contains("'" + dmRelationship + "'", rule.Message);
            StringAssert.Contains(" on the class '" + dmRelationship.ClassDM + "' ", rule.Message);
            StringAssert.Contains(" cannot be Dereference Related because the Relationship Type is Composition.", rule.Message);
        }

//        [Test]
//        public void Test_CreateDMRelationshipDeleteActionRule_WhenRelationshipNull_ShouldRaiseError()
//        {
//            //---------------Set up test pack-------------------
//            //---------------Assert Precondition----------------
//            //---------------Execute Test ----------------------
//            try
//            {
//                new DMRelationshipDeleteActionRule(null);
//                Assert.Fail("expected ArgumentNullException");
//            }
//            //---------------Test Result -----------------------
//            catch (ArgumentNullException ex)
//            {
//                StringAssert.Contains("Value cannot be null", ex.Message);
//                StringAssert.Contains("dmRelationship", ex.ParamName);
//            }
//        }

        [Test]
        public void Test_DMRelationshipDeleteActionRule_FailsWhenDereferencRelatedAndRelTypeIsComposition()
        {
            //---------------Set up test pack-------------------
            DMRelationship dmRelationship = TestUtilsShared.CreateUnsavedValidRelationshipInfo();
            dmRelationship.DeleteAction = DeleteParentAction.DereferenceRelated;
            dmRelationship.RelationshipType = RelationshipType.Composition;
            IBusinessObjectRule rule = new DMRelationshipDeleteActionRule();
            //---------------Assert Precondition----------------
            Assert.AreEqual( DeleteParentAction.DereferenceRelated, dmRelationship.DeleteAction);
            Assert.AreEqual( RelationshipType.Composition, dmRelationship.RelationshipType);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmRelationship);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            TestUtilsShared.AssertStringNotEmpty(rule.Message, "rule.Message");
        }

        [Test]
        public void Test_DMRelationshipDeleteActionRule_PassesWhenNotDereferencRelatedAndRelTypeIsComposition()
        {
            //---------------Set up test pack-------------------
            DMRelationship dmRelationship = TestUtilsShared.CreateUnsavedValidRelationshipInfo();
            dmRelationship.DeleteAction = DeleteParentAction.DoNothing;
            dmRelationship.RelationshipType = RelationshipType.Association;
            IBusinessObjectRule rule = new DMRelationshipDeleteActionRule();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmRelationship);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_DMRelationshipDeleteActionRule_PassesWhenDereferencRelatedAndRelTypeIsNotComposition()
        {
            //---------------Set up test pack-------------------
            DMRelationship dmRelationship = TestUtilsShared.CreateUnsavedValidRelationshipInfo();
            dmRelationship.DeleteAction = DeleteParentAction.DereferenceRelated;
            dmRelationship.RelationshipType = RelationshipType.Association;
            IBusinessObjectRule rule = new DMRelationshipDeleteActionRule();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmRelationship);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_DMRelationshipDeleteActionRule_PassesWhenNotDereferencRelatedAndRelTypeComposition()
        {
            //---------------Set up test pack-------------------
            DMRelationship dmRelationship = TestUtilsShared.CreateUnsavedValidRelationshipInfo();
            dmRelationship.DeleteAction = DeleteParentAction.DoNothing;
            dmRelationship.RelationshipType = RelationshipType.Composition;
            IBusinessObjectRule rule = new DMRelationshipDeleteActionRule();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmRelationship);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }
    }
}
